﻿using Microsoft.Win32;
using ModernUILogViewer.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace ModernUILogViewer.View
{
    public class BaseView : UserControl
    {
        public BaseView()
        {
            this.DataContextChanged += BaseView_DataContextChanged;
        }

        void BaseView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && e.NewValue is BaseViewModel)
            {
                BaseViewModel vm = (BaseViewModel)e.NewValue;

                vm.OpenSaveFileDlg =
                    (Func<string, string>)((ext) =>
                        {
                            SaveFileDialog dialog = new SaveFileDialog();
                            dialog.Filter = ext; 
                            dialog.ShowDialog();

                            return dialog.FileName;
                        });
                
                vm.ShowOpenFilesDlg =
                    (Func<bool, string[]>)((multiselect) =>
                        {
                            string[] filenames = null;

                            OpenFileDialog openFileDialog = new OpenFileDialog();                            
                            openFileDialog.Multiselect = multiselect;
                            if (openFileDialog.ShowDialog() == true)
                            {
                                filenames = openFileDialog.FileNames;
                            }

                            return filenames ?? new string[0];
                        }
                    );
            }
        }
    }
}
